import json
from elasticsearch import Elasticsearch

# 连接 Elasticsearch
es = Elasticsearch(
    ["http://39.108.176.217:9200"],
)

index_name = "my_index"

mapping = es.indices.get_mapping(index=index_name)
print(json.dumps(mapping, indent=4, ensure_ascii=False))
# print(mapping)
"""
{
    "my_index": {
        "mappings": {
            "properties": {
                "address": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "age": {
                    "type": "long"
                },
                "author": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "content": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "gender": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "views": {
                    "type": "long"
                }
            }
        }
    }
}


其中
"address": {
    "type": "text",
    "fields": {
        "keyword": {
            "type": "keyword",
            "ignore_above": 256
        }
    }
}
模糊搜索（会分词）
GET /your_index/_search
{
  "query": {
    "match": {
      "address": "中关村"
    }
  }
}
精确匹配（keyword不会进行分词）
GET /your_index/_search
{
  "query": {
    "term": {
      "address.keyword": "北京市海淀区中关村南大街5号"
    }
  }
}
按地址分组，统计每个地址有多少个用户（聚合）
GET /your_index/_search
{
  "size": 0,
  "aggs": {
    "addresses": {
      "terms": {
        "field": "address.keyword",
        "size": 10
      }
    }
  }
}

建议提前定义好所有字段的 Mapping，避免依赖动态推断
举例
PUT /iam
{
  "mappings": {
    "properties": {
      "info":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email":{
        "type": "keyword",
        "index": "false"
      },
      "name":{
        "properties": {
          "firstName": {
            "type": "keyword"
          }
        }
      }
    }
  }
}
firstName是name的子字段


具体的后续继续学习
"""